﻿using System;
using System.Windows.Forms;
using SolutionExtender.Properties;
using System.Xml;

namespace SolutionExtender.Controls
{
    public partial class ActionSelectionControl : UserControl
    {
        public ActionSelectionControl()
        {
            InitializeComponent();
        }

        public bool IsExport()
        {
            return rdbExport.Checked;
        }

        private void BtnBrowseClick(object sender, System.EventArgs e)
        {
            var ofd = new OpenFileDialog
            {
                Title = "Select a file to import",
                Filter = "Xml file (*.xml)|*.xml"
            };

            if (ofd.ShowDialog(this) == DialogResult.OK)
            {
                txtSettingsFilePath.Text = ofd.FileName;
                pnlError.Visible = false;
            }
        }

        public XmlNodeList GetXmlNodeToImport()
        {
            if (rdbImport.Checked && txtSettingsFilePath.Text.Length == 0)
            {
                pnlError.Visible = true;
            }
            else
            {
                try
                {
                    var doc = new XmlDocument();
                    doc.Load(txtSettingsFilePath.Text);

                    return doc.DocumentElement.ChildNodes;
                }
                catch
                {
                    pnlError.Visible = true;
                }
            }

            return null;
        }

        private void RdbImportCheckedChanged(object sender, System.EventArgs e)
        {
            btnBrowse.Enabled = rdbImport.Checked;

            if(rdbExport.Checked)
                pnlError.Visible = false;
        }
    }
}
